package com.crm.service.impl;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.dao.BizProductPriceMapper;
import com.crm.model.BizProductPrice;
import com.crm.service.BizProductPriceService;
import com.crm.util.UUIDUtil;

@Service
public class BizProductPriceServiceImpl extends BaseServiceImpl<BizProductPrice> implements BizProductPriceService{
	
	@Autowired
	private BizProductPriceMapper bizProductPriceMapper;

	@Override
	@Transactional
	public void insertBatch(String price, String[] departments, String productId, String content) {
		
		String priceBatch  = UUIDUtil.getUUID();
		
		for(String departmentId : departments){
			BizProductPrice bizProductPrice = new BizProductPrice();
			bizProductPrice.setPrice(price);
			bizProductPrice.setProductId(productId);
			bizProductPrice.setPriceBatch(priceBatch);
			bizProductPrice.setDepartmentId(departmentId);
			bizProductPrice.setContent(content);
			
			if(this.selectUnion(bizProductPrice) == null)
				this.insert(bizProductPrice);
			else
				this.updateByUnion(bizProductPrice);
		}
		
	}
	
	@Override
	public int insert(BizProductPrice record) {
		
		String uuid = UUIDUtil.getUUID();
		record.setProductPriceId(uuid);
		
		return super.insert(record);
	}

	@Override
	public BizProductPrice selectUnion(BizProductPrice bizProductPrice) {
		return bizProductPriceMapper.selectByUnion(bizProductPrice);
	}

	@Override
	public int updateByUnion(BizProductPrice bizProductPrice) {
		return bizProductPriceMapper.updateByUnion(bizProductPrice);
	}

}
